package com.woniuxy.controller;

import com.woniuxy.entity.User;
import com.woniuxy.service.UserService;
import com.woniuxy.utils.Chinese;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserContrller {
    /*dispatcherServlet*/
    @Autowired
    Chinese chinese;
    @Autowired
    UserService userService;
    @RequestMapping("/findAll")
    public String findAll(Model model, ModelMap modelMap, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<User> user = userService.findAll();
        //req.setAttribute("data",user);
        //resp.sendRedirect("/findOneByLikeName");

        return chinese.toString();
        //return "forward:/findOneByLikeName";
    }
    @RequestMapping("findOneByLikeName")
    @ResponseBody
    public List findOneByLikeName(String name ,Model model){
        List<User> list = userService.findOneByLikeName(name);
        //model.addAttribute("data",list);
        return list;
    }
    /*Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: The expression 'ids' evaluated to a null value.*/
    @RequestMapping(value = "findByIds",method = RequestMethod.POST)
    public String findByIds(Integer[] ids,Model model){
        List<User> list = userService.findByIds(ids);
        model.addAttribute("data",list);
        return "success";
    }
    @RequestMapping(value = "/findByLists",method = RequestMethod.POST)
    public String findByLists(@RequestParam List<Integer> ids, Model model){
        Integer[] i =ids.toArray(new Integer[0]);
        List<User> list = userService.findByIds(i);
        model.addAttribute("data",list);
        return "success";
    }
    @RequestMapping(value = "/addOneForMap")
    public String addOneForMap(Map<String,Object> userinfo, Model model){
        if (userinfo.size()!=0) {
            int i = userService.addOneForMap(userinfo);
        }
        model.addAttribute("data","新增用户："+userinfo);
        return "success";
    }
    @RequestMapping("/addOne")
    public String addOne(User user,Model model){
        int y = userService.addOne(user);
        model.addAttribute("data","新增用户："+user);
        return "success";
    }
}
